package www.qiyue.pt;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class bianma {
@Autowired
private  JdbcTemplate jdbcTemplate;



    @RequestMapping("gl")
    public void test(){
        String house="SELECT * FROM ais_house WHERE house_code like 'E%' and resources_status='正常'";
        List<Map<String, Object>> mapList = jdbcTemplate.queryForList(house);
        String gl="SELECT sub_table_str FROM cgform_head where content='房屋信息'";
        Map<String, Object> mapgl = jdbcTemplate.queryForMap(gl);
        for (Map<String, Object> map : mapList) {

            String ais_house = addCode("ais_house", null, null);

            update(mapgl,ais_house,map.get("id").toString());

        }
    }

    private String addCode(String tableName,String fieldName,String house_id) {
        int parseInt;

        Map<String, Object> map = new HashMap<>();
        // 获取当前年份
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String format = sdf.format(new Date());
        if(tableName.equals("ais_contract")) {

            String sqlMax = "select MAX(SUBSTR( " + fieldName + " FROM LENGTH( " + fieldName + ")-3 FOR LENGTH( " + fieldName + "))) as maxCode from " + tableName + "  where  " + fieldName + " like '%-%'";
            return format + "-" + String.format("%04d", maxCode(sqlMax,fieldName));
        }else if(tableName.equals("ais_house")){
            String house="SELECT MAX(CAST(house_code as UNSIGNED INTEGER)) as maxCode FROM ais_house WHERE house_code not REGEXP '^[a-zA-Z]'";

            return maxCode(house,fieldName)+"";
        }else if(tableName.equals("ais_rent_unit")) {
            String rent_unit="SELECT house_code FROM ais_house WHERE id='"+house_id+"'";
            Map<String, Object> rent = jdbcTemplate.queryForMap(rent_unit);
            Object rent_unit_code = rent.get("house_code");
            String sqlMax = "select MAX(SUBSTR( " + fieldName + " FROM LENGTH( " + fieldName + ")-3 FOR LENGTH( " + fieldName + "))) as maxCode from " + tableName + "  where  " + fieldName + " like '%-%'";
            return rent_unit_code + "-" + String.format("%04d", maxCode(sqlMax,fieldName));
        }
        return null;
    }
    private Integer maxCode(String sql ,String fieldName){
        int parseInt;
        // 获取后4位编码最大值

        Map<String, Object> maxMap = new HashMap<>();
        maxMap = jdbcTemplate.queryForMap(sql);
        String queryForObject = maxMap.get("maxCode")+"";
        if (StringUtils.isNotBlank(queryForObject)) {
            if (StringUtils.isNumeric(queryForObject)) {
                parseInt = Integer.parseInt(queryForObject) + 1;
            } else {
                parseInt = 1;
            }
        } else {
            parseInt = 1;
        }
        return parseInt;
    }
    private void update(Map<String, Object> map,String code,String id){
        String s = map.get("sub_table_str") == null ? "" : map.get("sub_table_str").toString();
        String[] split = s.split(",");
        for (String s1 : split) {
            System.out.println(s1);
            String sql="SELECT * FROM "+s1+" WHERE house_id='"+id+"'";
            List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql);
            if (maps!=null&&maps.size()>0){
                String updates="UPDATE "+s1+" set house_code='"+code+"' WHERE house_id='"+id+"'";
                int update1 = jdbcTemplate.update(updates);

            }


        }

        String house="UPDATE ais_house set house_code='"+code+"' WHERE id='"+id+"'";
        jdbcTemplate.update(house);
    }
}
